<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar> 
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.parent.code })">
          {{ record.parent.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhRd01Rd01ListDetails">
  import { unref, watch, onMounted, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rds01ListData } from '/@/api/wms/wh/rd01/rd01';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { companyTreeData } from '/@/api/sys/company';
  import { TableSummary, TableSummaryCell, TableSummaryRow } from 'ant-design-vue';
  import { useUserStore } from '/@/store/modules/user';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { dateUtil } from '/@/utils/dateUtil';
  const userStore = useUserStore();

  const { t } = useI18n('wh.rd01.rd01');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('采购入库明细'),
  };

  const props = defineProps({
    code: { type: String, default: '' },
  });

  const iqty = computed(() => {
    let sumiqty = 0;
    for (const record of getDataSource()) {
      const { iqty } = record;
      sumiqty += Number(iqty || 0);
    }
    return sumiqty;
  });

  const fweight = computed(() => {
    let sumfweight = 0;
    for (const record of getDataSource()) {
      const { fweight } = record;
      sumfweight += Number(fweight || 0);
    }
    return sumfweight.toFixed(4);
  });

  watch(
    () => props.code,
    async () => {
      await getForm().setFieldsValue({
        'parent.code': props.code,
      });
      await reload();
    },
  );

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'parent.companyCode',
        fieldLabel: 'company.companyName',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('入库单号'),
        field: 'parent.code',
        component: 'Input',
      },
      {
        label: t('任务单号'),
        field: 'sourceId',
        component: 'Input',
      },
      {
        label: t('单据日期'),
        field: 'parent.ddate',
        component: 'RangePicker',
      },
      {
        label: t('业务类型'),
        field: 'parent.busType',
        component: 'Select',
        componentProps: {
          dictType: 'wh_rd01_bus_type',
          allowClear: true,
        },
      },
      {
        label: t('红蓝标志'),
        field: 'parent.bred',
        component: 'Select',
        componentProps: {
          dictType: 'wms_bred_status',
          allowClear: true,
        },
      },

      {
        label: t('仓库'),
        field: 'parent.basWare.cwhname',
        component: 'Input',
      },
      {
        label: t('商品编码'),
        field: 'basInv.viewCode',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('商品名称'),
        field: 'basInv.invName',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('形象刊'),
        field: 'freeVO.cfree1',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('制单时间'),
        field: 'parent.createDate',
        component: 'RangePicker',
        componentProps: {},
      },
      {
        label: t('制单人'),
        field: 'parent.createByName',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('备注信息'),
        field: 'parent.remarks',
        component: 'Input',
        componentProps: {},
      },
      {
        label: t('行备注'),
        field: 'remarks',
        component: 'Input',
        componentProps: {},
      },

        {
        label: t('推送状态'),
        field: 'parent.bsend',
        component: 'Select',
        componentProps: {
          dictType: 'sys_yes_no',
          allowClear: true,
        },
      },
       {
        label: t('审批状态'),
        field: 'parent.status',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
    ],
    fieldMapToTime: [['parent.ddate', ['parent.ddate_gte', 'parent.ddate_lte']],
    ['parent.createDate', ['parent.createDate_gte', 'parent.createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('公司'),
      dataIndex: 'parent.company.companyName',
      key: 'parent.company_code',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('入库单号'),
      dataIndex: 'parent.code',
      key: 'parent.code',
      sorter: true,
      width: 150,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('任务单号'),
      dataIndex: 'sourceId',
      key: 'a.source_id',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('单据日期'),
      dataIndex: 'parent.ddate',
      key: 'parent.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('业务类型'),
      dataIndex: 'parent.busType',
      key: 'parent.bus_type',
      sorter: true,
      width: 100,
      align: 'left',
      dictType: 'wh_rd01_bus_type',
    },
    {
      title: t('红蓝标志'),
      dataIndex: 'parent.bred',
      key: 'parent.bred',
      sorter: true,
      width: 90,
      align: 'center',
      dictType: 'wms_bred_status',
    },

    {
      title: t('仓库'),
      dataIndex: 'parent.basWare.cwhname',
      key: 'parent.wh_code',
      sorter: true,
      width: 130,
      align: 'left',
    },

    {
      title: t('商品编码'),
      dataIndex: 'basInv.viewCode',
      key: 'inv.inv_code',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('商品名称'),
      dataIndex: 'basInv.invName',
      key: 'inv.inv_name',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('单位'),
      dataIndex: 'basInv.funitname',
      key: 'inv.funitname',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('货位'),
      dataIndex: 'basPosition.posName',
      key: 'a.pos_code',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'center',
      showSummary: true,
      showTotalSummary: true,
    },
    // {
    //   title: t('重量'),
    //   dataIndex: 'fweight',
    //   key: 'a.fweight',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    // },
    {
      title: t('形象刊'),
      dataIndex: 'freeVO.cfree1',
      key: 'a.cfree1',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自由项2'),
      dataIndex: 'freeVO.cfree2',
      key: 'a.cfree2',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('自由项3'),
      dataIndex: 'freeVO.cfree3',
      key: 'a.cfree3',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单时间'),
      dataIndex: 'parent.createDate',
      key: 'parent.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('制单人'),
      dataIndex: 'parent.createByName',
      key: 'parent.create_by_name',
      sorter: true,
      width: 130,
      align: 'left',
    },

    {
      title: t('审批状态'),
      dataIndex: 'parent.status',
      key: 'parent.status',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'bpm_biz_status',
      fixed: 'right',
    },
    {
      title: t('备注信息'),
      dataIndex: 'parent.remarks',
      key: 'parent.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('行备注'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('推送状态'),
      dataIndex: 'parent.bsend',
      key: 'parent.bsend',
      sorter: true,
      width: 100,
      align: 'center',
      dictType: 'sys_yes_no',
    },
  ];

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource, setProps }] = useTable({
    api: rds01ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    showCustomSummary: true,
    showTotalCustomSummary: true
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/rd01/rd01/exportSubData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'flow_wh_rd01', bizKey: record.id });
  }

  onMounted(async () => {
    setProps()
    await getForm().setFieldsValue({
      'parent.code': props.code || '',
      'parent.companyCode': userStore.getProjecte.code,
    });
    await reload();
    await getForm().setFieldsValue({
      'parent.code': props.code || '',
      'parent.companyCode': userStore.getProjecte.code,
    });
    getHomePage();
  });

  async function getHomePage() {
    let date = dateUtil(new Date()).format('YYYY-MM-DD');
    let arr = [date, date];
    if(router.currentRoute.value.query.homePage == 'true'){
      console.log('首页');
      await getForm().setFieldsValue({
        'parent.bred': '1',
        'parent.createDate': arr,
      });
      reload();
    }
    if(router.currentRoute.value.query.homePage == 'true1'){
      await getForm().setFieldsValue({
        'parent.bred': '0',
        'parent.createDate': arr,
      });
      reload();
    }
  }
</script>
